package com.springinaction.ch06.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;


/**
 * @author ssk
 * @Title: WebConfig
 * @ProjectName SpringInAction
 * @Description: TODO
 * @date 18-12-7下午9:24
 */

@Configuration
@ComponentScan({"com.springinaction.ch06.web"})
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    /**
     *
     *  https://www.cnblogs.com/jiangtao1218/p/8588750.html 视图与解析器 ViewResolver和View
     *
     *
     *
     */
      @Bean
      /**
      　* @Description: viewResolver视图解析器 配置视图解析器，名为home的视图将被解析为/WEB-INF/views/home.jsp(Prefix + home+Suffix)
      　* @param []
      　* @return org.springframework.web.servlet.ViewResolver
      　* @throws
      　* @date 18-12-7 下午9:44
      　*/
      public ViewResolver viewResolver() {
          InternalResourceViewResolver resolver = new InternalResourceViewResolver();
          resolver.setPrefix("/ch06/");
          resolver.setSuffix(".jsp");
          resolver.setExposeContextBeansAsAttributes(true);
          resolver.setViewClass(JstlView.class);
          resolver.setOrder(3);
          return resolver;

      }
      @Bean
      public  ViewResolver tilesViewResolver() {
          return new TilesViewResolver();
      }
     // 开始配置thymeleaf

    /**
     *ThymeleafViewResolver：将逻辑视图名称解析为Thymeleaf 模板视图；
     * SpringTemplateEngine：处理模板并渲染结果；
     * TemplateResolver：加载Thymeleaf模板。
     *
     */
      @Bean
      public ViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine) {
          ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
          thymeleafViewResolver.setTemplateEngine(templateEngine);
          return thymeleafViewResolver;
      }

    /**
     *  Thymeleaf3使用ITemplateResolver接口，SpringResourceTemplateResolver实现类
     *  Thymeleaf3之前使用TemplateResolver接口，ServletContextTemplateResolver实现类
     *
     */
    @Bean
    public SpringTemplateEngine templateEngine(ITemplateResolver iTemplateResolver) {
        SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
        springTemplateEngine.setTemplateResolver(iTemplateResolver);
        return springTemplateEngine;
    }
    @Bean
    public ITemplateResolver iTemplateResolver(){
        SpringResourceTemplateResolver iTemplateResolver = new SpringResourceTemplateResolver();
        iTemplateResolver.setPrefix("/ch06/");
        iTemplateResolver.setSuffix(".jsp");
        iTemplateResolver.setTemplateMode("HTML5");
        return iTemplateResolver;
    }
    //结束
    @Override
    /**
    　* @Description: 配置静态文件处理方式 此配置要求DispatcherServlet将对静态资源的请求转发到Servlet容器中默认的Servlet上
        而非使用DispatcherServlet本身来处理此类请求
    　* @param [configurer]
    　* @return void
    　* @throws 
    　* @date 18-12-7 下午9:45
    　*/
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
           configurer.enable();
          //super.configureDefaultServletHandling(configurer);
    }


}
